package com.yxlgateway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxlgateway.filter.WordFilter;
import com.yxlcommon.common.mapper.*;
import com.yxlcommon.common.pojo.OrderComment;
import com.yxlcommon.common.pojo.Orders;
import com.yxlcommon.common.pojo.RespBean;
import com.yxlgateway.service.OrderCommentService;
import com.yxlcommon.common.vo.OrderCommentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.ZoneId;
import java.util.Date;

/**
 * @author: yxl
 * @since: 2022-01-14
 **/
@Slf4j
@Service
public class OrderCommentServiceImpl extends ServiceImpl<OrderCommentMapper, OrderComment> implements OrderCommentService {

    @Autowired
    private OrderCommentMapper orderCommentMapper;

    @Autowired
    private DemandsMapper demandsMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private DemandReceiveMapper demandReceiveMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    public RespBean addComment(OrderComment orderComment) {
        orderComment.setOrderCommentContent(WordFilter.replaceWords(orderComment.getOrderCommentContent()));
        orderComment.setCreatedTime(new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
        demandsMapper.updateStatus((byte) 3, orderComment.getDemandId());
        ordersMapper.updateStatus((byte) 5, orderComment.getOrderId());
        demandReceiveMapper.updateDemandStatus((byte) 1, orderComment.getDemandId());
        return RespBean.operationSuccess("添加成功", 200, orderCommentMapper.insert(orderComment));
    }

    @Override
    public RespBean updateReplyComment(OrderComment orderComment) {
        orderComment.setReplyContent(WordFilter.replaceWords(orderComment.getReplyContent()));
        orderCommentMapper.updateReplyCommentContent(orderComment.getOrderId(),orderComment.getReplyContent());
        ordersMapper.updateStatus((byte) 6, orderComment.getOrderId());
        orderCommentMapper.update(orderComment, new UpdateWrapper<OrderComment>().eq("order_id",orderComment.getOrderId()).set("reply_status",1).set("reply_time",new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime()));
        return RespBean.operationSuccess("更新成功！", 200, "ok");
    }

    @Override
    public RespBean<IPage<OrderComment>> selectAllOrderCommentsByPage(Integer currentPage, Integer size, OrderComment orderComment) {
        Page<OrderComment> page = new Page<>(currentPage,size);
        return RespBean.operationSuccess("分页查询订单评论成功！", 200, orderCommentMapper.selectAllOrderCommentsByPage(page,orderComment.getOrderCommentContent()));
    }

    @Override
    public RespBean<Integer> changeOrderCommentById(OrderComment orderComment) {
        orderComment.setReplyContent(WordFilter.replaceWords(orderComment.getReplyContent()));
        orderComment.setOrderCommentContent(WordFilter.replaceWords(orderComment.getOrderCommentContent()));
        if (orderCommentMapper.updateById(orderComment) == 1){
            return RespBean.operationSuccess("修改评论内容成功！", 200, 1);
        }
        return RespBean.operationFail("修改评论内容失败！");
    }

    @Override
    public RespBean<Integer> adminAddOrderComment(OrderComment orderComment) {
        if (orderCommentMapper.selectCount(new QueryWrapper<OrderComment>().eq("order_id",orderComment.getOrderId())) > 0){
            return RespBean.operationFail("订单已被评论！");
        }
        orderComment.setDemandId(ordersMapper.selectOne(new QueryWrapper<Orders>().eq("order_id",orderComment.getOrderId())).getDemandId());
        orderComment.setReplyContent(WordFilter.replaceWords(orderComment.getReplyContent()));
        orderComment.setOrderCommentContent(WordFilter.replaceWords(orderComment.getOrderCommentContent()));
        if (orderCommentMapper.insert(orderComment) == 1){
            return RespBean.operationSuccess("添加评论成功！", 200, 1);
        }
        return RespBean.operationFail("添加评论失败！");
    }

    @Override
    public RespBean<OrderCommentVo> selectCommentById(OrderComment orderComment) {
        return RespBean.operationSuccess("查询评论成功！", 200, orderCommentMapper.selectOrderCommentById(orderComment.getOrderCommentId()));
    }

    @Override
    public RespBean<Integer> deleteOrderCommentById(OrderComment orderComment) {
        if (orderCommentMapper.deleteById(orderComment) == 1){
            return RespBean.operationSuccess("删除评论成功！", 200, 1);
        }
        return RespBean.operationFail("删除评论失败！");
    }
}
